package com.bbg.amyam.web.mvc.controller.views.schoolmanagementview;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bbg.amyam.domain.model.ProgramCategory;
import com.bbg.amyam.domain.model.School;

@RequestMapping(value="/{id:[0-9a-z]{2,20}}/school_management_view/programcategory")
@Controller
public class SchoolManagementViewProgramcategoryController {

	@RequestMapping(produces = "text/html")
	public String list(Model uiModel, HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		uiModel.addAttribute("programcategorys", ProgramCategory 
				.findProgramCategorysBySchool(currentSchool).getResultList());
		return "school_management_view/programcategory/index";
	}

	@RequestMapping(value = "/{id}", produces = "text/html")
	public String show(@PathVariable Long id, Model uiModel) {
		uiModel.addAttribute("programcategory",
				ProgramCategory .findProgramCategory (id));
		uiModel.addAttribute("itemId", id);
		return "school_management_view/programcategory/show";
	}

	@RequestMapping(method = RequestMethod.POST, produces = "text/html")
	public String create(@Valid ProgramCategory programCategory,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		programCategory.setSchool(currentSchool);
		uiModel.asMap().clear();
		programCategory.persist();
		return "redirect:/"+currentSchool.getShortName()+"/school_management_view/programcategory?id=" + programCategory.getId();

	}

	@RequestMapping(params = "form", produces = "text/html")
	public String createForm(Model uiModel) {
		populateEditForm(uiModel, new ProgramCategory ());
		return "school_management_view/programcategory/create";
	}

	@RequestMapping(value = "/{id}", params = "form", produces = "text/html")
	public String updateForm(@PathVariable("id") Long id, Model uiModel,
			HttpServletRequest request) {
		School currentSchool = (School) request
				.getAttribute("current_school_obj");
		ProgramCategory entity = ProgramCategory.findProgramCategory(id);
		if (entity.getSchool().getId().longValue() != currentSchool.getId()
				.longValue()) {
			return "redirect:/"+currentSchool.getShortName()+"/school_management_view/programcategory";
		}
		populateEditForm(uiModel, entity);
		return "school_management_view/programcategory/update";
	}

	@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
	public String update(@Valid ProgramCategory programCategory,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		programCategory.setSchool(currentSchool);
		uiModel.asMap().clear();
		programCategory.merge();
		return "redirect:/"+currentSchool.getShortName()+"/school_management_view/programcategory?id=" + programCategory.getId();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
	public String delete(@PathVariable("id") Long id,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel, HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		ProgramCategory programCategory = ProgramCategory
				.findProgramCategory(id);
		programCategory.remove();
		uiModel.asMap().clear();
		uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
		uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/"+currentSchool.getShortName()+"/school_management_view/programcategory";
	}

	void populateEditForm(Model uiModel, ProgramCategory programCategory) {
		uiModel.addAttribute("programCategory", programCategory);
	}

}
